(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.popup.repaymentView.viewController", viewController);
	viewController.$inject = [ "$scope", "$q", "dialogsModule", "$uibModalInstance", "$filter", "layoutConfig", "validatorConfig", "gridService", "authService", "repaymentService", "settlementSummaryService", "settlementPaymentService", "glEntryService", "rolloverService" ];
	function viewController($scope, $q, dialogsModule, $uibModalInstance, $filter, layoutConfig, validatorConfig, gridService, authService, repaymentService, settlementSummaryService, settlementPaymentService, glEntryService, rolloverService) {
		var ctrl = this, settlement = {};
		ctrl.dialogsModule = dialogsModule;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.setlDetailsConfig = layoutConfig.formConfigs.setlDetailView.tableConfigs.setlDetails.columnDefs;
		ctrl.isDetailsPay = function(item){
			return settlementSummaryService.isDetailsPay(item);
		};
		
		ctrl.isDetailsReceive = function(item){
			return settlementSummaryService.isDetailsReceive(item);
		};
		
		ctrl.init = function() {	
			ctrl.txnSetlInsts = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.txnSetlInsts.tableConfigs.txnSetlInsts, null);
			ctrl.txnSetlInsts.data = "vm.settlement.txnSetlInsts";
			ctrl.txnSetlInsts.enableRowHeaderSelection = false;
			var defer1 = $q.defer(), defer2 = $q.defer();
			settlementSummaryService.getFxRateTypes().then(function(fxRateTypes){
				settlement.fxRateTypes = fxRateTypes;
				defer1.resolve();
			});
			settlementPaymentService.settlementPaymentOptions().then(function(map) {
				settlement.deliveryTypeOptions = map.deliveryTypeOptions;
				settlement.currencyOptions = map.currencyOptions;
				settlement.allPartyTypeOptions = map.partyTypeOptions;
				settlement.swiftCaseList = map.swiftCaseList;
				defer2.resolve();
			});
			($q.all({
				defer1 : defer1.promise,
				defer2 : defer2.promise
			})).then(function(){
				authService.viewInit(ctrl.dialogsModule.params).then(function(response) {
					return $q(function(resoved, rejected) {
						resoved(response);
					});
				}, submitFailed).then(function(response) {
					var repayment = response.data.repayment, defer1 = $q.defer(), defer2 = $q.defer();
					repaymentService.getLoanViewRepaymentLayout(repayment.loan.loanInfo.prodSubtype, repayment.loan.loanInfo.prodType).then(function(layout) {
						defer1.resolve(layout);
					});
					rolloverService.getRolloverProdCodeOptions({
						prodCode : repayment.loan.loanInfo.prodCode
					}).then(function(options) {
						defer2.resolve(options);
					});
					$q.all({
						layout : defer1.promise,
						options : defer2.promise
					}).then(function(result) {
						//repayment
						var repayment = response.data.repayment;
						repayment.rolloverProdCodeOptions = result.options;
						repaymentService.initData(repayment);
						repayment.payType_Show = true;
						//settlement
						settlement = angular.merge(settlement, response.data.settlement);
						processTxnSetlInsts(settlement);
						getGlEntryList(ctrl, settlement);
						angular.extend(ctrl, settlementPaymentService.getPayDetailsConfig(settlement, layoutConfig));
						settlementSummaryService.initData(settlement);
						ctrl.payPartyConfig = settlementPaymentService.initData(settlement, layoutConfig);
						ctrl.isSettlement = settlement.settlementInfo.setlId ? true : false;
						ctrl.layoutConfig = result.layout;
						ctrl.settlement = settlement;
						ctrl.repayment = repayment;
					});
				});
			});
		};
		
		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}
		
		ctrl.finalRepayFlagChanged = function(repayment){
			repaymentService.finalRepayFlagChanged(repayment);
		};
		
		ctrl.repayAmtChanged = function(repayment){
			repaymentService.repayAmtChanged(repayment);
		};
		
		ctrl.payPrincAmtChanged = function(repayment){
			repaymentService.payPrincAmtChanged(repayment);
		};
		
		ctrl.repayTypeChanged = function(repayment){
			repaymentService.repayTypeChanged(repayment, ctrl.layoutConfig.formConfigs.repayment.gridConfigs);
		};
		
		function getGlEntryList(ctrl, settlement){
			glEntryService.getGlentryList($scope, layoutConfig, settlement.settlementInfo.parentId, "CLS:" + settlement.settlementInfo.parentType).then(function(response){
				ctrl.glEntryListConfig = response.glEntryListConfig;
				ctrl.glEntryList = response.glEntryList;
			});
		};
		
		function processTxnSetlInsts(settlement){
			if(settlement.txnSetlInsts){
				angular.forEach(settlement.txnSetlInsts, function(data, index, array){
					data.paycodeName = data.paycode ? data.paycode.substr(7) : "";
					data.itemType_desc = $filter("translate")("label.settlement.init." + data.itemType.toLowerCase());
				});
			}
		}
		
		//settlement payment
		ctrl.swiftTypeCallBack = function(result){
			settlementPaymentService.swiftTypeCallBack(ctrl.settlement, layoutConfig, result);
		};
		
		ctrl.partyCountryChanged = function(payParty){
			settlementPaymentService.partyCountryChanged(ctrl.settlement, payParty);
		};
		
		ctrl.partyStateChanged = function(payParty){
			settlementPaymentService.partyStateChanged(ctrl.settlement, payParty);
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};
	}
})(angular);